package com.rattsoftware.server.serviceImpl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rattsoftware.server.admin.model.CourseUnit;
import com.rattsoftware.server.admin.model.exceptions.RattsSecurityException;
import com.rattsoftware.server.admin.model.paging.PagingLoadConfig;
import com.rattsoftware.server.admin.model.paging.PagingLoadResult;
import com.rattsoftware.server.dao.CourseUnitDAO;
import com.rattsoftware.server.service.CourseUnitService;

@Service("courseUnitService")
@Transactional
public class CourseUnitServiceImpl implements CourseUnitService{

	@Autowired
	private CourseUnitDAO courseUnitDAO;
	private Logger log = LoggerFactory.getLogger(CourseUnitServiceImpl.class);
	
	@Override
	public void deleteCourseUnit(CourseUnit courseUnit)
			throws RattsSecurityException {
		log.debug("Deleting course unit " + courseUnit.getName());
		courseUnitDAO.deleteCourseUnit(courseUnit);
	}

	@Override
	public void deleteCourseUnit(List<Integer> courseUnitIds)
			throws RattsSecurityException {
		for (Integer courseUnitId : courseUnitIds) {
			courseUnitDAO.deleteCourseUnit(getCourseUnit(courseUnitId));
		}
	}

	@Override
	public CourseUnit getCourseUnit(int courseUnitId)
			throws RattsSecurityException {
		return courseUnitDAO.getCourseUnit(courseUnitId);
	}

	@Override
	public PagingLoadResult<CourseUnit> getCourseUnitList(
			PagingLoadConfig pagingLoadConfig) throws RattsSecurityException {
		return courseUnitDAO.getCourseUnitList(pagingLoadConfig);
	}

	@Override
	public CourseUnit saveCourseUnit(CourseUnit courseUnit)
			throws RattsSecurityException {
		courseUnitDAO.saveCourseUnit(courseUnit);
		return courseUnit;
	}

	@Override
	public void saveCourseUnits(List<CourseUnit> courseUnits)
			throws RattsSecurityException {
		for( CourseUnit courseUnit : courseUnits){
			saveCourseUnit(courseUnit);
		}
	}
}
